/*
* License:  This  program  is  free  software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published by
* the  Free Software Foundation; either version 3 of the License, or (at your
* option)  any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*/

package pcgen.CharacterViewer.resources;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Parcel;
import android.os.Parcelable;

import pcgen.android.Logger;

public class GameResourceAttributeSkill extends GameResourceAttribute
{
	public GameResourceAttributeSkill()
	{	
		setType(Types.Skill);
	}
	
	public GameResourceAttributeSkill(Parcel in)
	{
		super(in);
		setType(Types.Skill);
	}
	
	public static final Parcelable.Creator<GameResourceAttributeSkill> CREATOR = new Parcelable.Creator<GameResourceAttributeSkill>() 
	{
		public GameResourceAttributeSkill createFromParcel(Parcel in) 
		{
		    return new GameResourceAttributeSkill(in);
		}
		
		public GameResourceAttributeSkill[] newArray(int size) 
		{
		    return new GameResourceAttributeSkill[size];
		}
	};
	
	@Override
	public int getModifier()
	{
		int value = super.getModifier();
		value = getFolder().checkConditionSkill(this, value);
		return value;
	}
	
	@Override
	public int getModifierTotal()
	{
		return getModifier() + getAbility().getModifier();
	}
	
	public double getRank()
	{
		return _rank;
	}
	
	public int getRankBase()
	{
		return new Double(Math.floor(_rank)).intValue();
	}
	
	public SkillTypes getSkillType()
	{
		return _skillType;
	}
	
	public boolean getUntrained()
	{
		return _untrained;
	}
	
	@Override
	public int getValue()
	{
		return getRankBase() + getMisc() + getAbilityModifier() + getModifier();
	}
	
	public boolean isReaction()
	{
		return (isTypeListen() || isTypeSpot());
	}
	
	public boolean isTypeListen()
	{
		return (_skillType == SkillTypes.Listen);
	}
	
	public boolean isTypeSearch()
	{
		return (_skillType == SkillTypes.Search);
	}
	
	public boolean isTypeSpot()
	{
		return (_skillType == SkillTypes.Spot);
	}
	
	@Override
	public JSONObject load(JSONObject data)
		throws JSONException
	{
		super.load(data);
		
		if (data == null)
			return null;

		setRank(getJSONDouble(data, "rank"));
		setSkillType(SkillTypes.valueOf(getJSONString(data, "skillType")));
		setUntrained(getJSONBoolean(data, "untrained"));
	    
	    return data;
	}
	
	@Override
	public JSONObject save() 
		throws JSONException
	{
	    JSONObject data = super.save();

	    data.put("rank", _rank);
	    data.put("skillType", _skillType.toString());
	    data.put("untrained", _untrained);
	    
	    return data;
	}
	
	public void setRank(double value)
	{
		_rank = value;
	}
	
	public void setSkillType(SkillTypes value)
	{
		_skillType = value;
	}
	
	public void setUntrained(int value)
	{
		_untrained = (value == 1);
	}
	
	public void setUntrained(boolean value)
	{
		_untrained = value;
	}
	
	public void setValue(String name, String ability, double rank, int misc, boolean untrained)
	{
		setName(name);
		setSkillType(name);
		setAbilityType(getFolder().getTypeAbilityId(ability));
		setRank(rank);
		setMisc(misc);
		setUntrained(untrained);
	}

	@Override
	protected void readFromParcelTransformEx(Parcel in)  
		throws Throwable
	{
		try
		{
			super.readFromParcelTransformEx(in);
			
			setRank(in.readDouble());
			setUntrained(in.readInt());
			setSkillType(SkillTypes.valueOf(in.readString()));
		}
    	catch (Throwable tr)
    	{
    		Logger.e(TAG, "readFromParcelTransformEx", tr);
    		throw tr;
    	}
	}

	@Override
	protected void writeToParcelTransformEx(Parcel dest, int flags)
		throws Throwable
	{
		try
		{
			super.writeToParcelTransformEx(dest, flags);
				
			dest.writeDouble(_rank);
			dest.writeInt(_untrained ? 1 : 0);
			dest.writeString(_skillType.toString());
		}
    	catch (Throwable tr)
    	{
    		Logger.e(TAG, "writeToParcelTransform", tr);
    		throw tr;
    	}
	}
	
	private void setSkillType(String name)
	{
		_skillType = SkillTypes.Other;
		
		if ("Listen".equalsIgnoreCase(name))
			_skillType = SkillTypes.Listen;
		else if ("Search".equalsIgnoreCase(name))
			_skillType = SkillTypes.Search;
		else if ("Spot".equalsIgnoreCase(name))
			_skillType = SkillTypes.Spot;
		else if ("Tumble".equalsIgnoreCase(name))
			_skillType = SkillTypes.Tumble;
	}

	private double _rank;
	private boolean _untrained;
	private SkillTypes _skillType;
	
	public enum SkillTypes
	{
		Listen,
		Other,
		Search,
		Spot,
		Tumble
	}

	private static final String TAG = GameResourceAttributeSkill.class.getSimpleName();
}
